// sign算法
var CryptoJS = require('./hmac-sha1');
var base64_encode = require('./base64_encode');
var qs = require('./qs');

var sign = function (method, domain, resource, timestamp, param) {
    var querystring = qs(param);
    var originStr = method + domain + resource + timestamp + querystring;
    // 如果是GET，那么签名的顺序有所不同
    if (method == 'GET') {
        originStr = method + domain + resource + (querystring ? '?' : '') + querystring + timestamp;
        // console.log(originStr);
    }
    // console.log(originStr);
    var base64Str = base64_encode(originStr);
    // var base64Str = CryptoJS.enc.Base64.stringify(originStr);
    // var sign = crypto.createHmac('sha1', secret).update(base64Str).digest().toString('base64');
    // console.log(sign)
    // var secret = '3600d0a74aa3410fb3b1996cca2419c8';
    //     var message = 'UE9TVGh0dHBzOi8vYXBpLmZjb2luLmNvbS92Mi9vcmRlcnMxNTIzMDY5NTQ0MzU5YW1vdW50PTEwMC4wJnByaWNlPTEwMC4wJnNpZGU9YnV5JnN5bWJvbD1idGN1c2R0JnR5cGU9bGltaXQ=';
    var sign = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(base64Str, wx.getStorageSync('secret')));
    // console.log(sign)
    return sign;
}

module.exports = sign;